// pages/join/join.js
var util = require('../../util/util.js');
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    newIdList:[],
    isShowList: []
  },
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  getOwn(start = 0){
    wx.showLoading({
      title: '加载中',
    })
    wx.cloud.callFunction({
      name: 'signUp',
      data: {
        $url: 'getOwn',
        start: start,
        count: 20
      }
    }).then((res) => {
      console.log(res.result.list)
      let idList = []
      for (let i = 0; i < res.result.list.length; i++) {
        console.log(res.result.list[i])
        if (res.result.list[i]._openid == app.globalData.openid){
          console.log(res.result.list[i].ownList[0])
          if (res.result.list[i].ownList[0]!=undefined){
            res.result.list[i].ownList[0].createTime = util.formatTime(new Date(res.result.list[i].ownList[0].createTime))
            idList.unshift(res.result.list[i].ownList[0])
          }
          
        }
      }
      console.log(idList)
      for (let j = 0; j < idList.length; j++) {
        // idList[j].createTime = util.formatTime(new Date(idList[j].createTime))
        idList[j].isEnd = util.compareTime(idList[j].auto)
        for (let k = j + 1; k < idList.length; k++) {
          if (idList[j]._id == idList[k]._id) {         
            idList.splice(k, 1);
            k--;
          }
        }
      }

      console.log(idList)
      this.setData({
        newIdList: this.data.newIdList.concat(idList)
      })
      wx.hideLoading()
      wx.stopPullDownRefresh()
    })
    
  },


  _isLogin() {
    wx.getSetting({
      success: (res) => {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: (res) => {
              this.setData({
                isShowList: true,
                newIdList: []
              })
              this.getOwn(0)
            }
          })
        }
      }
    })

  },

  goSignUp(e) {
    console.log(e.currentTarget.dataset)
    wx.navigateTo({
      url: `../signUp-detail/signUp-detail?activityId=${e.currentTarget.dataset.activityid}`,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this._isLogin()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      newIdList: []
    })
    this.getOwn(0)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getOwn(this.data.newIdList.length)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})